<?php
//微信用户相关

namespace App\Http\Controllers;

use App\Models\RiskModel;
use App\Repositories\DealRepository;
use Illuminate\Http\Request;
use App\Repositories\RiskRepository;

class RiskController extends BaseController
{

    /**
     * 风险列表
     * @param Request $request
     * @param RiskRepository $riskRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function list (Request $request, RiskRepository $riskRepository)
    {
        $params = $request->all();
        $list = @$riskRepository->getList($params);
        return $this->success($list);
    }

    /**
     * 保存风险
     * @param Request $request
     * @param RiskRepository $riskRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function save (Request $request, RiskRepository $riskRepository)
    {
        $allParams = $request->all();
        $rst = @$riskRepository->save($allParams);
        if ($rst) {
            return $this->success([]);
        }
        return $this->error(405, '保存失败，请稍后再试');
    }

    /**
     * 获取评测记录
     * @param Request $request
     * @param RiskRepository $riskRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkList (Request $request, RiskRepository $riskRepository)
    {
        $params = $request->all();
        $list = @$riskRepository->getCheckList($params);
        return $this->success($list);
    }

    /**
     * 保存风险自测
     * @param Request $request
     * @param RiskRepository $riskRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveRiskCheck (Request $request, RiskRepository $riskRepository, DealRepository $dealRepository)
    {
        $params = $request->all();
        $user = request()->user();
        $rst = @$riskRepository->saveRiskCheck($params, $user);
        if ($rst) {
            $dealRepository->setRiskCheckId($params['stock_code']);
            return $this->success([]);
        }
        return $this->error(405, '保存失败，请稍后再试');
    }

    public function saveRisk (Request $request, RiskRepository $riskRepository)
    {
        $params = $request->all();
        $user = request()->user();
        $rst = @$riskRepository->saveRisk($params, $user);
        if ($rst) {
            return $this->success([]);
        }
        return $this->error(405, '保存失败，请稍后再试');
    }

    /**
     * 获取评测记录详情
     * @param Request $request
     * @param RiskRepository $riskRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function riskCheckDetail (Request $request, RiskRepository $riskRepository)
    {
        $params = $request->all();
        $user = request()->user();
        $detail = @$riskRepository->getRiskCheckDetail($params, $user);
        if ($detail) {
            return $this->success($detail);
        }
        return $this->error(405, '获取评测记录失败，请稍后再试');
    }

    public function optionList (Request $request, RiskRepository $riskRepository)
    {
        $user = request()->user();
        $risks = @RiskModel::select('id as value', 'name as label')
            ->where('admin_id', $user['id'])
            ->get();
        return $this->success($risks);
    }

    public function delete (Request $request)
    {
        $id = $request->input('id');
        $rst = @RiskModel::where('id', $id)->delete();
        if ($rst) {
            return $this->success([]);
        }
        return $this->error(405, '删除失败，请稍后再试');
    }

    public function riskDetail (Request $request)
    {
        $id = $request->input('id');
        $risk = @RiskModel::find($id);
        if ($risk) {
            return $this->success($risk);
        }
        return $this->error(405, '数据获取失败');
    }
}
